package org.glow.changgou.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.order.dao.ReturnOrderMapper;
import org.glow.changgou.order.pojo.ReturnCause;
import org.glow.changgou.order.pojo.ReturnOrder;
import org.glow.changgou.order.pojo.ReturnOrderItem;
import org.glow.changgou.order.service.ReturnOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:glow
 * @Description:ReturnOrder业务层接口实现类
 *****/
@Service
public class ReturnOrderServiceImpl extends ServiceImpl<ReturnOrderMapper, ReturnOrder> implements ReturnOrderService {

    @Resource
    private ReturnOrderMapper returnOrderMapper;


    /**
     * ReturnOrder条件+分页查询
     * @param returnOrder 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<ReturnOrder> findPage(ReturnOrder returnOrder, int page, int size){
        Page<ReturnOrder> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<ReturnOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,returnOrder);
        return returnOrderMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * ReturnOrder分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<ReturnOrder> findPage(int page, int size){
        Page<ReturnOrder> pageInfo = new Page<>(page,size);

        return returnOrderMapper.selectPage(pageInfo,null);
    }

    /**
     * ReturnOrder条件查询
     * @param returnOrder
     * @return
     */
    @Override
    public List<ReturnOrder> findList(ReturnOrder returnOrder){

        LambdaQueryWrapper<ReturnOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,returnOrder);
        return returnOrderMapper.selectList(queryWrapper);
    }


    /**
     * ReturnOrder构建查询对象
     * @param returnOrder
     * @return
     */
    public LambdaQueryWrapper<ReturnOrder> getCondition(LambdaQueryWrapper<ReturnOrder> queryWrapper, ReturnOrder returnOrder){

        if(returnOrder!=null){
            // 服务单号
            if(!StringUtils.isEmpty(returnOrder.getId())){
                queryWrapper.eq(ReturnOrder::getId,returnOrder.getId());
//                    criteria.andEqualTo("id",returnOrder.getId());
            }
            // 订单号
            if(!StringUtils.isEmpty(returnOrder.getOrderId())){
//                    criteria.andEqualTo("orderId",returnOrder.getOrderId());
                    queryWrapper.eq(ReturnOrder::getOrderId,returnOrder.getOrderId());
            }
            // 申请时间
            if(!StringUtils.isEmpty(returnOrder.getApplyTime())){
//                    criteria.andEqualTo("applyTime",returnOrder.getApplyTime());
                    queryWrapper.eq(ReturnOrder::getApplyTime,returnOrder.getApplyTime());
            }
            // 用户ID
            if(!StringUtils.isEmpty(returnOrder.getUserId())){
//                    criteria.andEqualTo("userId",returnOrder.getUserId());
                    queryWrapper.eq(ReturnOrder::getUserId,returnOrder.getUserId());
            }
            // 用户账号
            if(!StringUtils.isEmpty(returnOrder.getUserAccount())){
//                    criteria.andEqualTo("userAccount",returnOrder.getUserAccount());
                    queryWrapper.eq(ReturnOrder::getUserAccount,returnOrder.getUserAccount());
            }
            // 联系人
            if(!StringUtils.isEmpty(returnOrder.getLinkman())){
//                    criteria.andEqualTo("linkman",returnOrder.getLinkman());
                    queryWrapper.eq(ReturnOrder::getLinkman,returnOrder.getLinkman());
            }
            // 联系人手机
            if(!StringUtils.isEmpty(returnOrder.getLinkmanMobile())){
                queryWrapper.eq(ReturnOrder::getLinkmanMobile,returnOrder.getLinkmanMobile());
//                    criteria.andEqualTo("linkmanMobile",returnOrder.getLinkmanMobile());
            }
            // 类型
            if(!StringUtils.isEmpty(returnOrder.getType())){
                queryWrapper.eq(ReturnOrder::getType,returnOrder.getType());
//                    criteria.andEqualTo("type",returnOrder.getType());
            }
            // 退款金额
            if(!StringUtils.isEmpty(returnOrder.getReturnMoney())){
                    queryWrapper.eq(ReturnOrder::getReturnMoney,returnOrder.getReturnMoney());
//                    criteria.andEqualTo("returnMoney",returnOrder.getReturnMoney());
            }
            // 是否退运费
            if(!StringUtils.isEmpty(returnOrder.getIsReturnFreight())){
                queryWrapper.eq(ReturnOrder::getIsReturnFreight,returnOrder.getIsReturnFreight());
//                    criteria.andEqualTo("isReturnFreight",returnOrder.getIsReturnFreight());
            }
            // 申请状态
            if(!StringUtils.isEmpty(returnOrder.getStatus())){
                queryWrapper.eq(ReturnOrder::getStatus,returnOrder.getStatus());
//                    criteria.andEqualTo("status",returnOrder.getStatus());
            }
            // 处理时间
            if(!StringUtils.isEmpty(returnOrder.getDisposeTime())){
                queryWrapper.eq(ReturnOrder::getDisposeTime,returnOrder.getDisposeTime());
//                    criteria.andEqualTo("disposeTime",returnOrder.getDisposeTime());
            }
            // 退货退款原因
            if(!StringUtils.isEmpty(returnOrder.getReturnCause())){
                queryWrapper.eq(ReturnOrder::getReturnCause,returnOrder.getReturnCause());
//                    criteria.andEqualTo("returnCause",returnOrder.getReturnCause());
            }
            // 凭证图片
            if(!StringUtils.isEmpty(returnOrder.getEvidence())){
                queryWrapper.eq(ReturnOrder::getEvidence,returnOrder.getEvidence());
//                    criteria.andEqualTo("evidence",returnOrder.getEvidence());
            }
            // 问题描述
            if(!StringUtils.isEmpty(returnOrder.getDescription())){
                queryWrapper.eq(ReturnOrder::getDescription,returnOrder.getDescription());
//                    criteria.andEqualTo("description",returnOrder.getDescription());
            }
            // 处理备注
            if(!StringUtils.isEmpty(returnOrder.getRemark())){
                queryWrapper.eq(ReturnOrder::getRemark,returnOrder.getRemark());
//                    criteria.andEqualTo("remark",returnOrder.getRemark());
            }
            // 管理员id
            if(!StringUtils.isEmpty(returnOrder.getAdminId())){
                queryWrapper.eq(ReturnOrder::getAdminId,returnOrder.getAdminId());
//                    criteria.andEqualTo("adminId",returnOrder.getAdminId());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        returnOrderMapper.deleteById(id);
    }

    /**
     * 修改ReturnOrder
     * @param returnOrder
     */
    @Override
    public void update(ReturnOrder returnOrder){
        returnOrderMapper.updateById(returnOrder);
    }

    /**
     * 增加ReturnOrder
     * @param returnOrder
     */
    @Override
    public void add(ReturnOrder returnOrder){
        returnOrderMapper.insert(returnOrder);
    }

    /**
     * 根据ID查询ReturnOrder
     * @param id
     * @return
     */
    @Override
    public ReturnOrder findById(Long id){
        return  returnOrderMapper.selectById(id);
    }

    /**
     * 查询ReturnOrder全部数据
     * @return
     */
    @Override
    public List<ReturnOrder> findAll() {
        return returnOrderMapper.selectList(null);
    }
}
